package com.lzw.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzw.anno.Log;
import com.lzw.mapper.admin.StudentManagementMapper;
import com.lzw.pojo.Dormitory;
import com.lzw.service.admin.DormitoryManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dormitory")
@CrossOrigin(origins = "http://localhost:7070")
public class DormitoryManagementController {

    @Autowired
    private DormitoryManagementService dormitoryManagementService;

    //根据条件查询公寓
    @GetMapping("/list")
    public PageInfo<Dormitory> listDormitory(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            Integer dormitoryId, Integer buildingId){
        PageHelper.startPage(pageNum,pageSize);
        List<Dormitory> list = dormitoryManagementService.listDormitory(pageNum, pageSize, dormitoryId, buildingId);
        return new PageInfo<>(list);
    }

    //添加公寓
    @Log
    @PostMapping("/add")
    public ResponseEntity<String> addDormitory(@RequestBody Dormitory dormitory){
        try {
            dormitoryManagementService.addDormitory(dormitory);
            return ResponseEntity.ok("添加公寓成功");
        } catch (IllegalArgumentException e){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        }
    }

    //根据Id删除公寓
    @Log
    @DeleteMapping("/delete/{ids}")
    public String deleteDormitory(@PathVariable List<Integer> ids){
        dormitoryManagementService.deleteDormitory(ids);
        return "删除公寓成功";
    }

    //根据Id查询公寓
    @GetMapping("/get/{id}")
    public Dormitory getDormitoryById(@PathVariable("id") int id){
        return dormitoryManagementService.getDormitoryById(id);
    }

}
